package com.snopy.database.redis;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;

import java.sql.Time;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname PipelineDemo
 * @description
 * @email 77912204@qq.com
 * @date 2022/02/25 17:49
 */
@Slf4j
public class PipelineDemo {

    public static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1,1,1000,
            TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(50000), new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) throws InterruptedException {

        JedisPool jedisPool = loadRedis();

        List<Thread> list= new ArrayList<>();
        for (int i = 1;i<300;i++){
            Jedis jedis = jedisPool.getResource();
            try {
                TimeUnit.MILLISECONDS.sleep(10);
                //Thread writer = new Thread(new Expire(jedis,"c"+i,i*10));
                Thread writer = new Thread(new Writer(jedis,"new"+i,"n"+i));
                list.add(writer);
                threadPool.execute(writer);
                log.info("inserted:{}",i);
            }finally {
                if (jedis!=null){
                    jedis.close();
                    //jedisPool.returnResource(jedis);
                }
            }
        }
        log.info("task over");
        for (Thread t:list) {
            t.join();
        }
        //pipeline
        //TimeUnit.SECONDS.sleep(20);
       /* log.info("pipeline开始");
        for (int i = 0;i<50000;i++){
            try (Jedis jedis = jedisPool.getResource()) {
                Pipeline pipelined = jedis.pipelined();
                pipelined.append("A" + i, "hello world!" + i);
                if (i % 10000 == 0) {
                    pipelined.sync();
                }

            }
            //jedisPool.returnResource(jedis);


        }
        //pipelined.append("K1","V1");
        //非pipeline
*/
        log.info("thread pool shutdown!");
        threadPool.shutdown();

        //connection.close();

    }

    static class Writer implements Runnable{
        Jedis jedis;
        String key;
        String val;
        Writer(Jedis jedis,String key,String val){
            this.jedis = jedis;
            this.key = key;
            this.val = val;
        }
        @Override
        public void run() {
            String set = this.jedis.set(key, val);
            log.info("{}:{}",key,set);
        }
    }
    static class Expire implements Runnable{
        Jedis jedis;
        String key;
        long val;
        Expire(Jedis jedis,String key,long val){
            this.jedis = jedis;
            this.key = key;
            this.val = val;
        }
        @Override
        public void run() {
            long expire = this.jedis.expire(key, val);
            log.info("{}:{}",key,expire);
        }
    }
    private static JedisPool loadRedis(){
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(500);
        poolConfig.setMinIdle(100);
        poolConfig.setMaxWait(Duration.ofSeconds(1));
        poolConfig.setTestOnBorrow(false);
        poolConfig.setTestOnReturn(false);
        return new JedisPool(poolConfig,"192.168.164.106", 6379,20000,"123456");
    }

/*    private void collectRedisInfo(){
        Map<String, String> infoMap = parseInfo(infoContent);

        stat.setUsed_cpu_sys(getIntValue(infoMap, "CPU.used_cpu_sys"));
        stat.setUsed_cpu_user(getIntValue(infoMap, "CPU.used_cpu_user"));

        stat.setBlocked_clients(getIntValue(infoMap, "Clients.blocked_clients"));
        stat.setConnected_clients(getIntValue(infoMap, "Clients.connected_clients"));
        stat.setUsed_memory(getIntValue(infoMap, "Memory.used_memory"));
        stat.setUsed_memory_rss(getIntValue(infoMap, "Memory.used_memory_rss"));

        //
        int cmd = getIntValue(infoMap, "Stats.total_commands_processed");
        int exp = getIntValue(infoMap, "Stats.expired_keys");
        int evt = getIntValue(infoMap, "Stats.evicted_keys");

        int hit = getIntValue(infoMap, "Stats.keyspace_hits");
        int mis = getIntValue(infoMap, "Stats.keyspace_misses");

        long lastTs = stat.getTimestamp();
        long thisTs = System.currentTimeMillis();
    }*/

    /*LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated
 algorithms (in order to save memory), so you can tune it for speed or
 accuracy. For default Redis will check five keys and pick the one that was
 used less recently, you can change the sample size using the following
 configuration directive.
            #
            # The default of 5 produces good enough results. 10 Approximates very closely
 true LRU but costs more CPU. 3 is faster but not very accurate.
*/
}
